<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/5.13.0/css/all.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"horsenliu.gitee.io","root":"/blog/","scheme":"Pisces","version":"7.8.0","exturl":false,"sidebar":{"position":"left","width":240,"display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"default"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="1. 命令行操作1234567891011121314151617-- 连接数据库mysql -uroot -p123456-- 查看所有数据库show databases;-- 切换数据库use school;-- 查看数据库中所有的表show tables;-- 查看表结构describe student；-- 新建数据库create database newdb;">
<meta property="og:type" content="article">
<meta property="og:title" content="02_数据库操作">
<meta property="og:url" content="http://horsenliu.gitee.io/blog/posts/5dfb91cd.html">
<meta property="og:site_name" content="霍森的笔记本">
<meta property="og:description" content="1. 命令行操作1234567891011121314151617-- 连接数据库mysql -uroot -p123456-- 查看所有数据库show databases;-- 切换数据库use school;-- 查看数据库中所有的表show tables;-- 查看表结构describe student；-- 新建数据库create database newdb;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/5dfb91cd/sql_joins.jpg">
<meta property="og:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/5dfb91cd/image-20200725152825843.png">
<meta property="og:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/5dfb91cd/image-20200725153113879.png">
<meta property="article:published_time" content="2020-07-24T09:40:00.000Z">
<meta property="article:modified_time" content="2021-07-28T12:59:39.127Z">
<meta property="article:author" content="Horsen Liu">
<meta property="article:tag" content="CRUD">
<meta property="article:tag" content="MySQL">
<meta property="article:tag" content="select 查询语句">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://horsenliu.gitee.io/blog/blog/posts/blog/posts/5dfb91cd/sql_joins.jpg">

<link rel="canonical" href="http://horsenliu.gitee.io/blog/posts/5dfb91cd.html">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>02_数据库操作 | 霍森的笔记本</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">霍森的笔记本</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记的不少 会的不多</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">123</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">36</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">220</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/horsenliu" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://horsenliu.gitee.io/blog/posts/5dfb91cd.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.png">
      <meta itemprop="name" content="Horsen Liu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="霍森的笔记本">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          02_数据库操作
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-07-24 17:40:00" itemprop="dateCreated datePublished" datetime="2020-07-24T17:40:00+08:00">2020-07-24</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" itemprop="url" rel="index"><span itemprop="name">数据库学习笔记</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/MySQL/" itemprop="url" rel="index"><span itemprop="name">MySQL</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>6.2k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="1-命令行操作"><a href="#1-命令行操作" class="headerlink" title="1. 命令行操作"></a>1. 命令行操作</h3><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 连接数据库</span></span><br><span class="line">mysql <span class="operator">-</span>uroot <span class="operator">-</span>p123456</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看所有数据库</span></span><br><span class="line"><span class="keyword">show</span> databases;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 切换数据库</span></span><br><span class="line">use school;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看数据库中所有的表</span></span><br><span class="line"><span class="keyword">show</span> tables;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看表结构</span></span><br><span class="line"><span class="keyword">describe</span> student；</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 新建数据库</span></span><br><span class="line"><span class="keyword">create</span> database newdb;</span><br></pre></td></tr></table></figure>

<span id="more"></span>

<h3 id="2-结构化查询语句分类"><a href="#2-结构化查询语句分类" class="headerlink" title="2. 结构化查询语句分类"></a>2. 结构化查询语句分类</h3><table>
<thead>
<tr>
<th>名称</th>
<th>解释</th>
<th>命令</th>
</tr>
</thead>
<tbody><tr>
<td>DDL（数据定义语言）</td>
<td>定义和管理数据对象，如数据库、数据表等</td>
<td>create、drop、alter</td>
</tr>
<tr>
<td>DML（数据操作语言）</td>
<td>操作数据库对象中所包含的数据</td>
<td>insert、update、delete</td>
</tr>
<tr>
<td>DQL（数据查询语言）</td>
<td>查询数据库数据</td>
<td>select</td>
</tr>
<tr>
<td>DCL（数据控制语言）</td>
<td>管理数据库的语言，包括管理权限及数据更改</td>
<td>grant、commit、rollback</td>
</tr>
</tbody></table>
<h3 id="3-操作数据库"><a href="#3-操作数据库" class="headerlink" title="3. 操作数据库"></a>3. 操作数据库</h3><ol>
<li><p>创建数据库</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> database [if <span class="keyword">not</span> <span class="keyword">exists</span>] xxx;</span><br></pre></td></tr></table></figure>

<p>查看数据库的定义语句（逆向）</p>
<p><code>show create database xxx</code></p>
</li>
<li><p>删除数据库</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> database [if <span class="keyword">exists</span>] xxx;</span><br></pre></td></tr></table></figure>
</li>
<li><p>使用数据库</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">use `xxx`;</span><br><span class="line"><span class="comment">-- 如果表明或字段名为关键字 则需要加``</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>查看数据库</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> database;</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="4-操作数据库中的表"><a href="#4-操作数据库中的表" class="headerlink" title="4. 操作数据库中的表"></a>4. 操作数据库中的表</h3><ol start="0">
<li><p>数据库表的字段属性</p>
<table>
<thead>
<tr>
<th>属性</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>Unsigned</td>
<td>无符号整数（表示不能为负数）</td>
</tr>
<tr>
<td>Zerofill</td>
<td>0填充（不足位数自动补0）</td>
</tr>
<tr>
<td>自增</td>
<td>通常用来设计唯一主键（必须是整数类型）</td>
</tr>
<tr>
<td>非空</td>
<td>not null：不赋值报错    null：默认值为null</td>
</tr>
<tr>
<td>默认</td>
<td>默认值</td>
</tr>
</tbody></table>
</li>
<li><p>创建数据库表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> [if <span class="keyword">not</span> <span class="keyword">exists</span>] `student`(</span><br><span class="line">    `id` <span class="type">int</span>(<span class="number">4</span>) <span class="keyword">not</span> <span class="keyword">null</span> auto_increment comment <span class="string">&#x27;学号&#x27;</span>,</span><br><span class="line">    `name` <span class="type">varchar</span>(<span class="number">30</span>) <span class="keyword">not</span> <span class="keyword">null</span> <span class="keyword">default</span> <span class="string">&#x27;匿名&#x27;</span> comment <span class="string">&#x27;姓名&#x27;</span>,</span><br><span class="line">    `pwd` <span class="type">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="keyword">null</span> <span class="keyword">default</span> <span class="string">&#x27;123456&#x27;</span> comment <span class="string">&#x27;密码&#x27;</span>,</span><br><span class="line">    `sex` <span class="type">varchar</span>(<span class="number">2</span>) <span class="keyword">not</span> <span class="keyword">null</span> <span class="keyword">default</span> <span class="string">&#x27;男&#x27;</span> comment <span class="string">&#x27;性别&#x27;</span>,</span><br><span class="line">    `birthday` datetime <span class="keyword">default</span> <span class="keyword">null</span> comment <span class="string">&#x27;出生日期&#x27;</span>,</span><br><span class="line">    `address` <span class="type">varchar</span>(<span class="number">100</span>) <span class="keyword">default</span> <span class="keyword">null</span> comment <span class="string">&#x27;家庭住址&#x27;</span>,</span><br><span class="line">    `email` <span class="type">varchar</span>(<span class="number">50</span>) <span class="keyword">default</span> <span class="keyword">null</span> comment <span class="string">&#x27;邮箱&#x27;</span>,</span><br><span class="line">    <span class="keyword">primary</span> key (`id`)</span><br><span class="line">)engine<span class="operator">=</span>innodb <span class="keyword">default</span> charset<span class="operator">=</span>utf8</span><br></pre></td></tr></table></figure>

<p>查看数据库表的定义语句（逆向）</p>
<p><code>show create table xxx</code></p>
</li>
<li><p>显示表的结构</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">desc</span> xxx</span><br></pre></td></tr></table></figure>
</li>
<li><p>修改数据库表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 修改表名</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> oldName rename <span class="keyword">as</span> newName</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 增加表的字段</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> teacher <span class="keyword">add</span> age <span class="type">int</span>(<span class="number">3</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 修改表的字段</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> teacher modify age <span class="type">varchar</span>(<span class="number">11</span>) <span class="comment">-- 修改约束</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> teacher change age age1 <span class="type">int</span>(<span class="number">3</span>) <span class="comment">-- 字段重命名</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 删除表的字段</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> teacher <span class="keyword">drop</span> age1</span><br></pre></td></tr></table></figure>
</li>
<li><p>删除数据库表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">drop</span> <span class="keyword">table</span> [if <span class="keyword">exists</span>] teacher</span><br></pre></td></tr></table></figure></li>
</ol>
<h3 id="5-操作表的数据"><a href="#5-操作表的数据" class="headerlink" title="5. 操作表的数据"></a>5. 操作表的数据</h3><h4 id="5-1-添加外键（了解）"><a href="#5-1-添加外键（了解）" class="headerlink" title="5.1 添加外键（了解）"></a>5.1 添加外键（了解）</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> `student` </span><br><span class="line"><span class="keyword">add</span> <span class="keyword">constraint</span> `FK_gradeId` <span class="keyword">foreign</span> key(`gradeId`) <span class="keyword">references</span> `grade`(`gradeId`)</span><br></pre></td></tr></table></figure>

<h4 id="5-2-添加"><a href="#5-2-添加" class="headerlink" title="5.2 添加"></a>5.2 添加</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> `grade`(`gradeName`) <span class="keyword">values</span>(<span class="string">&#x27;大四&#x27;</span>)</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> `grade`(`gradeName`) <span class="keyword">values</span>(<span class="string">&#x27;大一&#x27;</span>),(<span class="string">&#x27;大二&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> `student`(`name`,`pwd`,`sex`) <span class="keyword">values</span>(<span class="string">&#x27;张三&#x27;</span>,<span class="string">&#x27;aaaa&#x27;</span>,<span class="string">&#x27;女&#x27;</span>)</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> `student`(`name`,`pwd`,`sex`) <span class="keyword">values</span>(<span class="string">&#x27;王五&#x27;</span>,<span class="string">&#x27;cccc&#x27;</span>,<span class="string">&#x27;女&#x27;</span>),(<span class="string">&#x27;李四&#x27;</span>,<span class="string">&#x27;bbbb&#x27;</span>,<span class="string">&#x27;男&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h4 id="5-3-修改"><a href="#5-3-修改" class="headerlink" title="5.3 修改"></a>5.3 修改</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> `student` <span class="keyword">set</span> `name`<span class="operator">=</span><span class="string">&#x27;lhx&#x27;</span> <span class="keyword">where</span> id<span class="operator">=</span><span class="number">1</span></span><br><span class="line"><span class="keyword">update</span> `student` <span class="keyword">set</span> `name`<span class="operator">=</span><span class="string">&#x27;lhx1&#x27;</span>,`email`<span class="operator">=</span><span class="string">&#x27;horsenliu@qq.com&#x27;</span> <span class="keyword">where</span> id<span class="operator">=</span><span class="number">1</span></span><br></pre></td></tr></table></figure>

<h4 id="5-4-删除"><a href="#5-4-删除" class="headerlink" title="5.4 删除"></a>5.4 删除</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">delete</span> <span class="keyword">from</span> `student` <span class="keyword">where</span> id<span class="operator">=</span><span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 清空一个数据库表</span></span><br><span class="line"><span class="keyword">truncate</span> `student`</span><br></pre></td></tr></table></figure>

<h3 id="6-查询数据（重点）"><a href="#6-查询数据（重点）" class="headerlink" title="6. 查询数据（重点）"></a>6. 查询数据（重点）</h3><h4 id="6-1-简单查询"><a href="#6-1-简单查询" class="headerlink" title="6.1 简单查询"></a>6.1 简单查询</h4><ol>
<li><p>别名</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> `studentNo` <span class="keyword">as</span> 学号,`studentName` <span class="keyword">as</span> 学生姓名 <span class="keyword">from</span> `student`</span><br></pre></td></tr></table></figure>
</li>
<li><p>拼接字符串</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> concat(<span class="string">&#x27;姓名:&#x27;</span>,`studentName`) <span class="keyword">as</span> 新名字 <span class="keyword">from</span> `student`</span><br></pre></td></tr></table></figure>
</li>
<li><p>去重</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> `studentNo` <span class="keyword">from</span> `<span class="keyword">result</span>`</span><br></pre></td></tr></table></figure>
</li>
<li><p>排除</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> `studentNo`,`studentResult` <span class="keyword">from</span> `<span class="keyword">result</span>`</span><br><span class="line"><span class="keyword">where</span> <span class="keyword">not</span> `studentNo`<span class="operator">=</span><span class="number">1000</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>模糊查询</p>
<table>
<thead>
<tr>
<th>运算符</th>
<th>语法</th>
</tr>
</thead>
<tbody><tr>
<td>is null</td>
<td>xxx is null</td>
</tr>
<tr>
<td>is not null</td>
<td>xxx is not null</td>
</tr>
<tr>
<td>between</td>
<td>xxx between 90 and 100</td>
</tr>
<tr>
<td>like</td>
<td>a like b</td>
</tr>
<tr>
<td>in</td>
<td>xxx in (a, b, c)</td>
</tr>
</tbody></table>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询姓刘的同学</span></span><br><span class="line"><span class="keyword">select</span> `studentNo`,`studentResult` <span class="keyword">from</span> `<span class="keyword">result</span>`</span><br><span class="line"><span class="keyword">where</span> `studentName` <span class="keyword">like</span> <span class="string">&#x27;刘%&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询姓刘的名字后面只有一个字的同学</span></span><br><span class="line"><span class="keyword">select</span> `studentNo`,`studentResult` <span class="keyword">from</span> `<span class="keyword">result</span>`</span><br><span class="line"><span class="keyword">where</span> `studentName` <span class="keyword">like</span> <span class="string">&#x27;刘_&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询姓刘的名字后面只有两个字的同学</span></span><br><span class="line"><span class="keyword">select</span> `studentNo`,`studentResult` <span class="keyword">from</span> `<span class="keyword">result</span>`</span><br><span class="line"><span class="keyword">where</span> `studentName` <span class="keyword">like</span> <span class="string">&#x27;刘__&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询名字中有嘉字的同学</span></span><br><span class="line"><span class="keyword">select</span> `studentNo`,`studentResult` <span class="keyword">from</span> `<span class="keyword">result</span>`</span><br><span class="line"><span class="keyword">where</span> `studentName` <span class="keyword">like</span> <span class="string">&#x27;%嘉%&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询1001,1002,1003学员</span></span><br><span class="line"><span class="keyword">select</span> `studentNo`,`studentResult` <span class="keyword">from</span> `<span class="keyword">result</span>`</span><br><span class="line"><span class="keyword">where</span> `studentNo` <span class="keyword">in</span> (<span class="number">1001</span>,<span class="number">1002</span>,<span class="number">1003</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询地址为空的学生</span></span><br><span class="line"><span class="keyword">select</span> `studentNo`,`studentResult` <span class="keyword">from</span> `<span class="keyword">result</span>`</span><br><span class="line"><span class="keyword">where</span> `address`<span class="operator">=</span><span class="string">&#x27;&#x27;</span> <span class="keyword">or</span> `address` <span class="keyword">is</span> <span class="keyword">null</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="6-2-联表查询"><a href="#6-2-联表查询" class="headerlink" title="6.2 联表查询"></a>6.2 联表查询</h4><img src="/blog/blog/posts/blog/posts/5dfb91cd/sql_joins.jpg" class>

<table>
<thead>
<tr>
<th>操作</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td>inner join</td>
<td>交集</td>
</tr>
<tr>
<td>left join</td>
<td>返回左表中所有的值，即使右表中没有匹配</td>
</tr>
<tr>
<td>right join</td>
<td>返回右表中所有的值，即使左表中没有匹配</td>
</tr>
</tbody></table>
<p>   <strong>SQL 语句编写思路：</strong></p>
<ol>
<li><p><strong>分析需求，分析查询的字段来自哪些表</strong></p>
</li>
<li><p><strong>确定使用哪种连接查询</strong></p>
</li>
<li><p><strong>确定交叉点（两张表中的相同数据）</strong></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询参加了考试的同学</span></span><br><span class="line"><span class="keyword">select</span> s.studentNo,studentName,subjectNo,subjectResult</span><br><span class="line"><span class="keyword">from</span> student <span class="keyword">as</span> s</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> <span class="keyword">result</span> <span class="keyword">as</span> r</span><br><span class="line"><span class="keyword">on</span> s.studentNo <span class="operator">=</span> r.studentNo</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询没有考试的同学</span></span><br><span class="line"><span class="keyword">select</span> s.studentNo,studentName,subjectNo,subjectResult</span><br><span class="line"><span class="keyword">from</span> student <span class="keyword">as</span> s</span><br><span class="line"><span class="keyword">left</span> <span class="keyword">join</span> <span class="keyword">result</span> <span class="keyword">as</span> r</span><br><span class="line"><span class="keyword">on</span> s.studentNo <span class="operator">=</span> r.studentNo</span><br><span class="line"><span class="keyword">where</span> subjectResult <span class="keyword">is</span> <span class="keyword">null</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询参加了考试的同学信息（学号、学生姓名、科目名、分数）</span></span><br><span class="line"><span class="keyword">select</span> s.studentNo,studentName,subjectName,studentResult</span><br><span class="line"><span class="keyword">from</span> student s</span><br><span class="line"><span class="keyword">right</span> <span class="keyword">join</span> <span class="keyword">result</span> r</span><br><span class="line"><span class="keyword">on</span> r.studentNo <span class="operator">=</span> s.studentNo</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> subject sub</span><br><span class="line"><span class="keyword">on</span> sub.subjectNo <span class="operator">=</span> r.subjectNo</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询学员所属的年级（学号、学生姓名、年级名称）</span></span><br><span class="line"><span class="keyword">select</span> studentNo,studentName,gradeName</span><br><span class="line"><span class="keyword">from</span> student s</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> grade g</span><br><span class="line"><span class="keyword">on</span> s.gradeId <span class="operator">=</span> g.gradeId</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询科目所属的年级（科目名称、年级名称）</span></span><br><span class="line"><span class="keyword">select</span> subjectName，gradeName</span><br><span class="line"><span class="keyword">from</span> subject sub</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> grade g</span><br><span class="line"><span class="keyword">on</span> sub.gradeId <span class="operator">=</span> g.gradeId</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询参加了 数据库结构-1 考试的同学信息（学号、学生姓名、科目名、分数）</span></span><br><span class="line"><span class="keyword">select</span> s.studentNo,studentName,subjectName,studentResult</span><br><span class="line"><span class="keyword">from</span> student s</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> <span class="keyword">result</span> r</span><br><span class="line"><span class="keyword">on</span> s.studentNo <span class="operator">=</span> r.studentNo</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> subject sub</span><br><span class="line"><span class="keyword">on</span> r.subjectNo <span class="operator">=</span> sub.subjectNo</span><br><span class="line"><span class="keyword">where</span> subjectName <span class="operator">=</span> <span class="string">&#x27;数据库结构-1&#x27;</span></span><br></pre></td></tr></table></figure></li>
</ol>
<h4 id="6-3-自连接"><a href="#6-3-自连接" class="headerlink" title="6.3 自连接"></a>6.3 自连接</h4><p>举一个例子：</p>
<p>category 表如下所示</p>
<img src="/blog/blog/posts/blog/posts/5dfb91cd/image-20200725152825843.png" class>

<p>想要将这一张表拆开，使父栏目和子栏目的对应关系清晰可见</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> a.`categoryName` <span class="keyword">AS</span> <span class="string">&#x27;父栏目&#x27;</span>,b.`categoryName` <span class="keyword">AS</span> <span class="string">&#x27;子栏目&#x27;</span> </span><br><span class="line"><span class="keyword">FROM</span> `category` <span class="keyword">AS</span> a,`category` <span class="keyword">AS</span> b </span><br><span class="line"><span class="keyword">WHERE</span> a.`categoryId` <span class="operator">=</span> b.`pid`</span><br></pre></td></tr></table></figure>

<img src="/blog/blog/posts/blog/posts/5dfb91cd/image-20200725153113879.png" class>

<h4 id="6-4-分页和排序"><a href="#6-4-分页和排序" class="headerlink" title="6.4 分页和排序"></a>6.4 分页和排序</h4><ul>
<li>升序 –&gt; ASC</li>
<li>降序 –&gt; DESC</li>
<li>分页公式：<ul>
<li>当前页 n</li>
<li>页面大小 m</li>
<li>起始下标 (n-1)*m</li>
<li>总页数 &#x3D; ⌈数据总数&#x2F;页面大小⌉</li>
<li><strong>limit (起始下标, 页面大小)</strong></li>
</ul>
</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询参加了 数据库结构-1 考试的同学信息</span></span><br><span class="line"><span class="comment">-- 按成绩降序排序</span></span><br><span class="line"><span class="comment">-- 每页只显示5条数据</span></span><br><span class="line"><span class="comment">-- limit 0,5 0-起始下标 5-显示数目</span></span><br><span class="line"><span class="keyword">select</span> s.studentNo,studentName,subjectName,studentResult</span><br><span class="line"><span class="keyword">from</span> student s</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> <span class="keyword">result</span> r</span><br><span class="line"><span class="keyword">on</span> s.studentNo <span class="operator">=</span> r.studentNo</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> subject sub</span><br><span class="line"><span class="keyword">on</span> r.subjectNo <span class="operator">=</span> sub.subjectNo</span><br><span class="line"><span class="keyword">where</span> subjectName <span class="operator">=</span> <span class="string">&#x27;数据库结构-1&#x27;</span></span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> studentResult <span class="keyword">desc</span></span><br><span class="line">limit <span class="number">0</span>,<span class="number">5</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询 JAVA第一学年 课程成绩排名前十并且分数大于80的学生信息（学号、姓名、课程名称、分数）</span></span><br><span class="line"><span class="keyword">select</span> s.studentNo,studentName,subjectName,studentResult</span><br><span class="line"><span class="keyword">from</span> student s</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> <span class="keyword">result</span> r</span><br><span class="line"><span class="keyword">on</span> s.studentNo <span class="operator">=</span> r.studentNo</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> subject sub</span><br><span class="line"><span class="keyword">on</span> sub.subjectNo <span class="operator">=</span> r.subjectNo</span><br><span class="line"><span class="keyword">where</span> studentName <span class="operator">=</span> <span class="string">&#x27;JAVA第一学年&#x27;</span> <span class="keyword">and</span> studentResult <span class="operator">&gt;=</span> <span class="number">80</span></span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> studentResult <span class="keyword">desc</span></span><br><span class="line">limit <span class="number">0</span>,<span class="number">10</span></span><br></pre></td></tr></table></figure>

<h4 id="6-5-子查询和嵌套查询"><a href="#6-5-子查询和嵌套查询" class="headerlink" title="6.5 子查询和嵌套查询"></a>6.5 子查询和嵌套查询</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 查询 数据库结构-1 的所有考试结果（学号、科目编号、成绩）降序</span></span><br><span class="line"><span class="comment">-- 方式一：使用连接查询</span></span><br><span class="line"><span class="keyword">select</span> s.studentNo,r.subjectNo,studentResult</span><br><span class="line"><span class="keyword">from</span> <span class="keyword">result</span> r</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> subject sub</span><br><span class="line"><span class="keyword">on</span> r.subjectNo <span class="operator">=</span> sub.subjectNo</span><br><span class="line"><span class="keyword">where</span> subjectName <span class="operator">=</span> <span class="string">&#x27;数据库结构-1&#x27;</span></span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> studentResult <span class="keyword">desc</span></span><br><span class="line"><span class="comment">-- 方式二：使用子查询</span></span><br><span class="line"><span class="keyword">select</span> s.studentNo,r.subjectNo,studentResult</span><br><span class="line"><span class="keyword">from</span> <span class="keyword">result</span> r</span><br><span class="line"><span class="keyword">where</span> subjectNo <span class="operator">=</span> (</span><br><span class="line">    <span class="keyword">select</span> subjectNo <span class="keyword">from</span> subject</span><br><span class="line">    <span class="keyword">where</span> subjectName <span class="operator">=</span> <span class="string">&#x27;数据库结构-1&#x27;</span></span><br><span class="line">)</span><br><span class="line"><span class="keyword">order</span> <span class="keyword">by</span> studentResult <span class="keyword">desc</span></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查询 高等数学-2 分数不小于80的学生的学号、姓名</span></span><br><span class="line"><span class="comment">-- 方式一：连接查询</span></span><br><span class="line"><span class="keyword">select</span> s.studentNo,studentName</span><br><span class="line"><span class="keyword">from</span> student s</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> <span class="keyword">result</span> r</span><br><span class="line"><span class="keyword">on</span> s.studentNo <span class="operator">=</span> r.studentNo</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> subject sub</span><br><span class="line"><span class="keyword">on</span> r.subjectNo <span class="operator">=</span> sub.subjectNo</span><br><span class="line"><span class="keyword">where</span> subjectName <span class="operator">=</span> <span class="string">&#x27;高等数学-2&#x27;</span> <span class="keyword">and</span> studentResult <span class="operator">&gt;=</span> <span class="number">80</span></span><br><span class="line"><span class="comment">-- 方式二：子查询</span></span><br><span class="line"><span class="keyword">select</span> <span class="keyword">distinct</span> s.studentNo,studentName</span><br><span class="line"><span class="keyword">from</span> student s</span><br><span class="line"><span class="keyword">inner</span> <span class="keyword">join</span> <span class="keyword">result</span> r</span><br><span class="line"><span class="keyword">on</span> r.studentNo <span class="operator">=</span> s.studentNo</span><br><span class="line"><span class="keyword">where</span> studentResult <span class="operator">&gt;=</span> <span class="number">80</span> <span class="keyword">and</span> stubjectNo <span class="operator">=</span> (</span><br><span class="line">    <span class="keyword">select</span> subjectNo <span class="keyword">from</span> subject <span class="keyword">where</span> subjectName<span class="operator">=</span><span class="string">&#x27;高等数学-2&#x27;</span></span><br><span class="line">)</span><br><span class="line"><span class="comment">-- 方式三：嵌套查询</span></span><br><span class="line"><span class="keyword">select</span> studentNo,studentName <span class="keyword">from</span> student</span><br><span class="line"><span class="keyword">where</span> studentNo <span class="keyword">in</span> (</span><br><span class="line">    <span class="keyword">select</span> studentNo <span class="keyword">from</span> <span class="keyword">result</span></span><br><span class="line">    <span class="keyword">where</span> studentResult <span class="operator">&gt;=</span> <span class="number">80</span> <span class="keyword">and</span> subjectNo <span class="operator">=</span> (</span><br><span class="line">        <span class="keyword">select</span> subjectNo <span class="keyword">from</span> subject</span><br><span class="line">        <span class="keyword">where</span> subjectName <span class="operator">=</span> <span class="string">&#x27;高等数学&#x27;</span></span><br><span class="line">    )</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Horsen Liu
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://horsenliu.gitee.io/blog/posts/5dfb91cd.html" title="02_数据库操作">http://horsenliu.gitee.io/blog/posts/5dfb91cd.html</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/blog/tags/CRUD/" rel="tag"><i class="fa fa-tag"></i> CRUD</a>
              <a href="/blog/tags/MySQL/" rel="tag"><i class="fa fa-tag"></i> MySQL</a>
              <a href="/blog/tags/select-%E6%9F%A5%E8%AF%A2%E8%AF%AD%E5%8F%A5/" rel="tag"><i class="fa fa-tag"></i> select 查询语句</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/posts/16b0a39b.html" rel="prev" title="01_安装 MySQL">
      <i class="fa fa-chevron-left"></i> 01_安装 MySQL
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/posts/1ab39822.html" rel="next" title="03_关于数据库表的引擎">
      03_关于数据库表的引擎 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%93%8D%E4%BD%9C"><span class="nav-text">1. 命令行操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-%E7%BB%93%E6%9E%84%E5%8C%96%E6%9F%A5%E8%AF%A2%E8%AF%AD%E5%8F%A5%E5%88%86%E7%B1%BB"><span class="nav-text">2. 结构化查询语句分类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-text">3. 操作数据库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-%E6%93%8D%E4%BD%9C%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%AD%E7%9A%84%E8%A1%A8"><span class="nav-text">4. 操作数据库中的表</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E6%93%8D%E4%BD%9C%E8%A1%A8%E7%9A%84%E6%95%B0%E6%8D%AE"><span class="nav-text">5. 操作表的数据</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#5-1-%E6%B7%BB%E5%8A%A0%E5%A4%96%E9%94%AE%EF%BC%88%E4%BA%86%E8%A7%A3%EF%BC%89"><span class="nav-text">5.1 添加外键（了解）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-2-%E6%B7%BB%E5%8A%A0"><span class="nav-text">5.2 添加</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-3-%E4%BF%AE%E6%94%B9"><span class="nav-text">5.3 修改</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5-4-%E5%88%A0%E9%99%A4"><span class="nav-text">5.4 删除</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-%E6%9F%A5%E8%AF%A2%E6%95%B0%E6%8D%AE%EF%BC%88%E9%87%8D%E7%82%B9%EF%BC%89"><span class="nav-text">6. 查询数据（重点）</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#6-1-%E7%AE%80%E5%8D%95%E6%9F%A5%E8%AF%A2"><span class="nav-text">6.1 简单查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-2-%E8%81%94%E8%A1%A8%E6%9F%A5%E8%AF%A2"><span class="nav-text">6.2 联表查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-3-%E8%87%AA%E8%BF%9E%E6%8E%A5"><span class="nav-text">6.3 自连接</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-4-%E5%88%86%E9%A1%B5%E5%92%8C%E6%8E%92%E5%BA%8F"><span class="nav-text">6.4 分页和排序</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#6-5-%E5%AD%90%E6%9F%A5%E8%AF%A2%E5%92%8C%E5%B5%8C%E5%A5%97%E6%9F%A5%E8%AF%A2"><span class="nav-text">6.5 子查询和嵌套查询</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Horsen Liu"
      src="/blog/images/avatar.png">
  <p class="site-author-name" itemprop="name">Horsen Liu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">220</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">36</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">123</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/horsenliu" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;horsenliu" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://wpa.qq.com/msgrd?v=3&uin=1468224254&site=qq&menu=yes" title="QQ → https:&#x2F;&#x2F;wpa.qq.com&#x2F;msgrd?v&#x3D;3&amp;uin&#x3D;1468224254&amp;site&#x3D;qq&amp;menu&#x3D;yes" rel="noopener" target="_blank"><i class="fab fa-qq fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://sm.ms/image/jyArGeatQ9Plw4n" title="WeChat → https:&#x2F;&#x2F;sm.ms&#x2F;image&#x2F;jyArGeatQ9Plw4n" rel="noopener" target="_blank"><i class="fab fa-weixin fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:horsenliu@qq.com" title="E-Mail → mailto:horsenliu@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Horsen Liu</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">707k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">10:43</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div><script color="252,100,35" pointColor="252,100,35" opacity="1" zIndex="-1" count="150" src="https://cdn.jsdelivr.net/npm/canvas-nest.js@1/dist/canvas-nest.js"></script>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>

<script src="/blog/js/bookmark.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

<script src="/blog/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/blog/live2dw/assets/assets/hijiki.model.json"},"display":{"superSample":2,"width":180,"height":360,"position":"left","hOffset":40,"vOffset":-60},"mobile":{"show":false,"scale":0.1},"react":{"opacityDefault":0.7,"opacityOnHover":0.8},"log":false});</script></body>
</html>
